package sudoku_gui_handler;

import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JLabel;
import sudoku_gui.SudokuComponentsProperties;

public class TimerHandler extends JLabel{

    public TimerHandler(){
        timer=new Timer();
        timerTask = new TimerHandlerTask(this);
        timer.schedule(timerTask, delay, WaitPeriod);
        this.setForeground(SudokuComponentsProperties.getColorSpecial());
    }

    /********************************
     ** Start/Stop countup timer   **
     ********************************/
    public void StartTimer(){
        timerTask.setTimerOn();
    }

    public void StopTimer(){
        timerTask.setTimerOff();
    }

    public void ReSet(){
        this.setText("00:00:00");
        timerTask.ResetTimer();
    }

    public String getDisplayTime(){
        return timerTask.getStringTimeValue();
    }

    // 20091112 phrase2 start
    public int getTimeValueInSecond(){
        return timerTask.getTimerValue();
    }
    // 20091112 phrase2 end 

    /* local variables */
    Timer timer;
    TimerHandlerTask timerTask;
    final int WaitPeriod=1000;
    final int delay=0;

}

class TimerHandlerTask extends TimerTask {

    public TimerHandlerTask(JLabel inputTimer){
        timer=inputTimer;
        timerValue=0;
        this.setTimerOff();
    }

    public void run() {
        String TimeElapsed;
        if(this.isTimerOn()){
            TimeElapsed=this.getStringTimeValue();
            timer.setText(TimeElapsed);
            this.timer.repaint();
            this.TimerTick();
        }
    }

    /**********************************
     ** support function for run()   **
     **********************************/
    public String getStringTimeValue(){

        String TimeString="";

        final int IntTimeValue = this.getTimerValue();
        String StrHr = Integer.toString(IntTimeValue/ NumSecperHr) ;
        String StrMin = Integer.toString(( IntTimeValue % NumSecperHr) / NumSecperMin) ;
        String StrSec = Integer.toString(IntTimeValue % NumSecperMin) ;

        if (StrHr.length() == 1){
            StrHr = "0"+StrHr;
        }
        if (StrMin.length() == 1){
            StrMin = "0"+StrMin;
        }
        if (StrSec.length() == 1){
            StrSec = "0"+StrSec;
        }

        TimeString = StrHr + ":" + StrMin+ ":" + StrSec;

        return TimeString ;
    }

    public int getTimeValueInSecond(){
        return this.getTimerValue();
    }

    /***************************************
     ** check/set timer common function   **
     ***************************************/

    public synchronized boolean isTimerOn()
    {
        return isTimerOn ;
    }

    public synchronized void setTimerOn()
    {
        isTimerOn=true;
    }

    public synchronized void setTimerOff()
    {
        isTimerOn=false;
    }

    public synchronized int getTimerValue()
    {
        return timerValue ;
    }

    private synchronized void TimerTick()
    {
        ++timerValue;
    }

    public synchronized void ResetTimer()
    {
        timerValue=0;
    }

    /* local variables */
    private int timerValue;
    private boolean isTimerOn;
    private JLabel timer;

    /*****************************************************
     ** constant variable for calculating time string   **
     *****************************************************/
    final Integer NumSecperMin = 60;
    final Integer NumSecperHr = 3600;

}
